package com.hw.protocol.tool.decode.hb;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.hw.protocol.tool.config.DevExceptionEnum;
import com.hw.protocol.tool.model.entity.MeterData;
import com.hw.protocol.tool.model.entity.hb.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author : lihy
 * @date : 2023/11/3 10:11
 * description: 华北协议数据转换
 */
public class HbTransformUtil {

    /**
     * 流量计数据转换为实时数据
     *
     * @param flower 流量计数据
     * @return 实时数据
     */
    public static MeterData transform(Flower flower) {
        MeterData md = getMeterData(flower.getDevSerial(), flower.getChunnelSerial() == 0 ? "5000" : "5100");
        md.setData1((double) flower.getTemperature());
        md.setData2((double) flower.getPress());
        md.setData3((double) flower.getFlux());
        md.setData4((double) flower.getRealFlux());
        md.setData5(flower.getVolume());
        md.setData6(flower.getRealVolume());
        md.setData9((double) flower.getRatio());
        md.setData10((double) flower.getFactor());
        md.setData12((double) (flower.getSignal() & 0xFF));
        String alarmMsg = "";
        if (flower.getMeterType() == 0) {
            //未读到数据
            alarmMsg = DevExceptionEnum.UNREAD_DATA.getCode() + ";";
        }
        md.setAlarmMsg(alarmMsg);
        md.setWriteTime(DateTime.of(flower.getTime(), DatePattern.NORM_DATETIME_PATTERN));
        return md;
    }

    /**
     * 调压箱数据转换为实时数据
     *
     * @param box 调压箱数据
     * @return 实时数据
     */
    public static MeterData transform(RegulatorBox box) {
        MeterData md = getMeterData(box.getDevSerial(), "6010");
        md.setData1((double) box.getInPress());
        md.setData2((double) box.getOutPress());
        md.setData3((double) box.getTemperature());
        md.setData4((double) box.getDifferentialPressure1());
        md.setData5((double) box.getDifferentialPressure2());
        md.setData6((double) (box.getValveOpening1() & 0xFF));
        md.setData7((double) (box.getValveOpening2() & 0xFF));
        md.setData8((double) box.getValveSsv1());
        md.setData9((double) box.getValveSsv2());
        md.setData10((double) box.getLeakage());
        md.setData12((double) (box.getSignal() & 0xFF));
        md.setData15((double) box.getDoorSignal1());
        md.setData16((double) box.getDoorSignal1());

        md.setWriteTime(DateTime.of(box.getTime(), DatePattern.NORM_DATETIME_PATTERN));

        return md;
    }

    /**
     * 泄漏数据转实时数据
     *
     * @param leakage 泄漏数据
     * @return 实时数据
     */
    public static MeterData transform(Leakage leakage) {
        MeterData md = getMeterData(leakage.getDevSerial(), "3000");
        md.setData1((double) leakage.getValue());
        md.setData2((double) leakage.getPress());
        md.setData3((double) leakage.getLevelAlarm());
        md.setData4((double) (leakage.getDip() & 0xFF));
        md.setData5((double) leakage.getLidAlarm());
        md.setData12((double) (leakage.getSignal() & 0xFF));
        md.setWriteTime(DateTime.of(leakage.getTime(), DatePattern.NORM_DATETIME_PATTERN));

        return md;
    }

    /**
     * 阴极保护数据转实时数据
     *
     * @param cp 阴极保护数据
     * @return 实时数据
     */
    public static MeterData transform(CathodeProtection cp) {
        MeterData md = getMeterData(cp.getDevSerial(), "6021");
        md.setData1((double) cp.getPowerOnPotential());
        md.setData2((double) cp.getPowerOffPotential());
        md.setData3((double) cp.getNaturePotential());
        md.setData4((double) cp.getAcInterferencePotential());
        md.setData5((double) cp.getAcInterferenceCurrent());
        md.setData6((double) cp.getDcInterferencePotential());
        md.setData7((double) cp.getDcInterferenceCurrent());
        md.setData8((double) cp.getAnodePotential());
        md.setData9((double) cp.getAnodeCurrent());
        md.setData12((double) (cp.getSignal() & 0xFF));
        md.setWriteTime(DateTime.of(cp.getTime(), DatePattern.NORM_DATETIME_PATTERN));
        return md;
    }

    /**
     * 报警器数据转实时数据
     *
     * @param alertor 报警器数据
     * @return 实时数据
     */
    public static List<MeterData> transform(Alertor alertor) {
        List<MeterData> list = new LinkedList<>();
        for (int i = 0; i < alertor.getAlerts().length; i++) {
            AlertorData alert = alertor.getAlerts()[i];
            if (alert.getSenser() == 0) {
                continue;
            }
            MeterData md = new MeterData();
            md.setDevSerial(alertor.getDevSerial());
            md.setChunnelSerial((10001 + i) + "");
            md.setData1((double) alert.getData());
            md.setData2((double) alert.getSenser());
            md.setData3((double) alert.getStatus());
            //md.setData4((double) alert.getType());
            md.setData12((double) (alertor.getSignal() & 0xFF));

            md.setWriteTime(DateTime.of(alertor.getTime(), DatePattern.NORM_DATETIME_PATTERN));
            //md.setHistory(true);

            list.add(md);
        }
        return list;
    }

    /**
     * 创建实时数据
     *
     * @param devSerial     设备编号
     * @param chunnelSerial 通道号
     * @return 实时数据
     */
    private static MeterData getMeterData(String devSerial, String chunnelSerial) {
        MeterData md = new MeterData();
        md.setDevSerial(devSerial);
        md.setChunnelSerial(chunnelSerial);
        return md;
    }
}
